package com.SchoolGuide;

//队列
public class LinkQueue<T>{
    static final int defaultSize = 20;
    T[]queueArray;
    int front;
    int rear;
    //初始化一个队列
    public LinkQueue(int n) {
        queueArray = (T[]) new Object[n];
        this.front = 0;
        this.rear=0;
    }
    public LinkQueue(){
        this(defaultSize);
    }
    //判断队列是否满 循环队列判断是否队满，看最后的rear再加一看是否等于front
    public void enQueue(T e){
        if ((rear%queueArray.length)==front){
            //循环队列已满从新建一个更大的空间。
            T[] p= (T[]) new Object[queueArray.length*2];
            int k=0;
            for (int i = front; i !=rear; i=(i+1)% queueArray.length) {
                p[k++] = queueArray[i];
            }
            queueArray=p;
            this.front=0;
            this.rear=k;
        }
        queueArray[this.rear]=e;
        this.rear=(this.rear+1)% queueArray.length;
    }
    //出队
    public T delQueue(){
        if (this.front==this.rear){
            return null;
        }
        T e = queueArray[front];
        this.front=(this.front+1)% queueArray.length;
        return e;
    }
    //判空
    public boolean isEmpty(){
        return this.front==this.rear;
    }

}
